//
//  MyOrderViewController.m
//  firstpoint
//
//  Created by 李雪虎 on 2017/7/6.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "MyOrderViewController.h"
#import "LeeOrderContenViewController.h"
@interface MyOrderViewController ()<UIScrollViewDelegate>
{
    
    NSInteger   _currentIndex;
    NSDictionary*dataDic;
    NSMutableArray*nuMarr;
    NSArray*arrrrrrr;
    UIView *_lineView;
}
@property (nonatomic,strong)UIButton *lastBtn;
@property(nonatomic,strong) UIScrollView *scrollView;//
@end

@implementation MyOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.title=@"我的订单";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initControl];
    [self addfristController];
    [self adddView];
    // Do any additional setup after loading the view.
}
-(void)adddView{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, width_screen, 40)];
    view.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:view];
    NSArray*arr=@[@"元宝",@"金币",@"充值记录",@"提现记录"];
    int aa = (width_screen-120)/4;
    for (int i=0; i<4; i++) {
        
        UILabel *tishiLable = [[UILabel alloc]initWithFrame:CGRectMake(aa+40+(aa+40)*i, 12, 30, 15)];
        tishiLable.backgroundColor = LRRGBColor(22, 141, 210);
        tishiLable.textColor = [UIColor whiteColor];
        tishiLable.layer.masksToBounds = YES;
        tishiLable.layer.cornerRadius = 7.0;
        //        tishiLable.text = nuMarr[i];
        tishiLable.textAlignment = NSTextAlignmentCenter;
        tishiLable.font = [UIFont systemFontOfSize:11];
        // [view addSubview:tishiLable];
        
        
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(width_screen/4*i, 0,width_screen/4, 40);
        [button setTitle:arr[i] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor whiteColor]];
        button.tag = 10+i;
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitleColor:LRRGBColor(68, 68, 68) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClicke:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
    }
    _lastBtn = [self.view viewWithTag:10];
    [_lastBtn setTitleColor:LRRGBColor(22, 141, 210) forState:UIControlStateNormal];
    _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 38,width_screen/arr.count ,2)];
    _lineView.backgroundColor = LRRGBColor(22, 141, 210);
    [view addSubview:_lineView];
}
-(void)buttonClicke:(UIButton *)button{
    
    [_lastBtn setTitleColor:LRRGBColor(68, 68, 68) forState:UIControlStateNormal];
    [button setTitleColor:LRRGBColor(22, 141, 210) forState:UIControlStateNormal];
    _lastBtn = button;
    [self yidongView];
    long a = button.tag-10;
    [self itemDidSelectedWithIndex:a withCurrentIndex:a];
    
    
}
#pragma mark Controller的创建
-(void)initControl{
    //1.全部,2.待付款,3.待发货,4.待收货,
    NSArray *urlarraystr = @[@"selfOrder/search.api",@"selfOrder/orderObligation.api",@"selfOrder/shipmentPending.api",@"selfOrder/orderShipped.api"];
    NSMutableArray *viewArray = [NSMutableArray array];
    for (int i=0; i<4; i++) {
        LeeOrderContenViewController *vc = [LeeOrderContenViewController new];
        vc.urlString = urlarraystr[i];
        [viewArray addObject:vc];
    }
    _subViewControllers = [NSArray array];
    _subViewControllers = viewArray;
}
//加载第一个
-(void)addfristController{
    [self addBigScrollView];
    
    UIViewController *viewController = (UIViewController *)_subViewControllers[0];
    viewController.view.frame = CGRectMake(0 , 10, width_screen , height_screen-104-49);
    [_scrollView addSubview:viewController.view];
    [self addChildViewController:viewController];
    
}
#pragma mark--滚动视图
-(void)addBigScrollView{
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.frame = CGRectMake(0, 94, width_screen, height_screen-94);
    _scrollView.backgroundColor=[UIColor whiteColor];
    _scrollView.contentSize=CGSizeMake(4 * width_screen, 0);
    _scrollView.delegate = self;
    _scrollView.showsVerticalScrollIndicator = FALSE;//隐藏横向滚动条
    _scrollView.showsHorizontalScrollIndicator = FALSE;//隐藏竖向滚动条
    _scrollView.alwaysBounceVertical = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO; //设置是否显示弹性效果
    _scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;//滑动
    [self.view addSubview:_scrollView];
    
}
#pragma 滚动视图协议方法实现


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    _currentIndex = scrollView.contentOffset.x / width_screen;
    /** 当scrollview滚动的时候加载当前视图 */
    UIViewController *viewController = (UIViewController *)_subViewControllers[_currentIndex];
    viewController.view.frame = CGRectMake(_currentIndex * width_screen, 10, width_screen, _scrollView.frame.size.height);
    [_scrollView addSubview:viewController.view];
    [self addChildViewController:viewController];
    
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
{
    NSLog(@"scrollViewDidEndDecelerating");
    _currentIndex = scrollView.contentOffset.x / width_screen;
    UIButton *button = (UIButton *)[self.view viewWithTag:_currentIndex+10];
    if (_lastBtn==button) {
        return;
    }else{
        [button setTitleColor:LRRGBColor(22, 141, 210) forState:UIControlStateNormal];
        [_lastBtn setTitleColor:LRRGBColor(68, 68, 68) forState:UIControlStateNormal];
        _lastBtn = button;
        [self yidongView];
    }
    
}
- (void)itemDidSelectedWithIndex:(NSInteger)index withCurrentIndex:(NSInteger)currentIndex
{
    if (currentIndex-index>=2 || currentIndex-index<=-2) {
        [_scrollView setContentOffset:CGPointMake(index * width_screen, 0) animated:NO];
    }else{
        [_scrollView setContentOffset:CGPointMake(index * width_screen, 0) animated:YES];
    }
}

-(void)yidongView
{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    _lineView.frame = CGRectMake(_lastBtn.frame.origin.x, 38,width_screen/4 ,2);
    NSLog(@"%f",_lastBtn.frame.origin.x);
    [UIView commitAnimations];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
